package com.turman.builibrary.banner;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.turman.builibrary.R;
import com.turman.builibrary.banner.core.BBannerDelegate;
import com.turman.builibrary.banner.core.BBannerModel;
import com.turman.builibrary.banner.core.IBBanner;
import com.turman.builibrary.banner.core.IBindAdapter;
import com.turman.builibrary.banner.indicator.IBIndicator;

import java.util.List;

/**
* BBanner实现
* @author buobao
* @since 2024/3/18 16:48
*/
public class BBanner extends FrameLayout implements IBBanner {
    private BBannerDelegate delegate; //主要逻辑放在代理类中实现

    public BBanner(@NonNull Context context) {
        this(context, null);
    }

    public BBanner(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BBanner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        delegate = new BBannerDelegate(getContext(), this);
        initCustomAttrs(context, attrs);
    }

    private void initCustomAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BBanner);
        boolean autoPlay = typedArray.getBoolean(R.styleable.BBanner_autoPlay, true);
        boolean loop = typedArray.getBoolean(R.styleable.BBanner_loop, false);
        int intervalTime = typedArray.getInteger(R.styleable.BBanner_intervalTime, -1);
        setAutoPlay(autoPlay);
        setLoop(loop);
        setInterval(intervalTime);
        typedArray.recycle();
    }

    @Override
    public void setBannerData(int layoutRes, @NonNull List<? extends BBannerModel> models) {
        delegate.setBannerData(layoutRes, models);
    }

    @Override
    public void setBannerData(@NonNull List<? extends BBannerModel> models) {
        delegate.setBannerData(models);
    }

    @Override
    public void setIndicator(@NonNull IBIndicator indicator) {
        delegate.setIndicator(indicator);
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        delegate.setAutoPlay(autoPlay);
    }

    @Override
    public void setLoop(boolean loop) {
        delegate.setLoop(loop);
    }

    @Override
    public void setInterval(int interval) {
        delegate.setInterval(interval);
    }

    @Override
    public void setBindAdapter(@NonNull IBindAdapter adapter) {
        delegate.setBindAdapter(adapter);
    }

    @Override
    public void setOnPageChangeListener(@NonNull ViewPager.OnPageChangeListener onPageChangeListener) {
        delegate.setOnPageChangeListener(onPageChangeListener);
    }

    @Override
    public void setOnBannerChickListener(@NonNull OnBannerClickListener onBannerChickListener) {
        delegate.setOnBannerChickListener(onBannerChickListener);
    }

    @Override
    public void setScrollDuration(int direction) {
        delegate.setScrollDuration(direction);
    }
}
